package com.hongning.railway.ui.splash;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.databinding.ActivitySplashBinding;
import com.hongning.railway.ui.login.LoginActivity;
import com.hongning.railway.ui.main.MainActivity;
import com.hongning.railway.util.Apputils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.utils.RxUtils;

public class SplashActivity extends BaseActivity<ActivitySplashBinding, BaseViewModel> {

    @Override
    public int initContentView(Bundle savedInstanceState) {
        //无title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
                WindowManager.LayoutParams. FLAG_FULLSCREEN);
        return R.layout.activity_splash;
    }

    @Override
    public int initVariableId() {
        return BR.splashmodel;
    }

    @Override
    public void initData() {
        Observable.interval(1, TimeUnit.SECONDS)
                .compose(this.<Long>bindToLifecycle())
                .compose(RxUtils.schedulersTransformer())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long along) throws Exception {
                      if (along==3){
                          if (Apputils.getLoginInfo()==null){
                              startActivity(LoginActivity.class);
                          }else {
                              startActivity(MainActivity.class);
                          }
                          finish();
                      }
                    }
                });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        //三秒后跳转至首页
//        if(!this.isTaskRoot()){
//            Intent mainIntent = getIntent();
//            String action = mainIntent.getAction();
//            if(mainIntent.hasCategory(Intent.CATEGORY_LAUNCHER)&&action.equals(Intent.ACTION_MAIN)){
//                finish();
//                return;
//            }
//        }
    }
}
